package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.service.ISysOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2022-01-19
 */
@Controller
@RequestMapping("/system/ordererror")
public class SysOrderErrorController extends BaseController
{
    private String prefix = "system/ordererror";

    @Autowired
    private ISysOrderService sysOrderService;

    @RequiresPermissions("system:ordererror:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/ordererror";
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("system:ordererror:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysOrder sysOrder)
    {
        startPage();
        sysOrder.setSysOrderStatusApi(2);
        List<SysOrder> list = sysOrderService.selectSysOrderList(sysOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("system:ordererror:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysOrder sysOrder)
    {
        List<SysOrder> list = sysOrderService.selectSysOrderList(sysOrder);
        ExcelUtil<SysOrder> util = new ExcelUtil<SysOrder>(SysOrder.class);
        return util.exportExcel(list, "渠道失败订单");
    }




    /**
     * 批量补单
     */
    @RequiresPermissions("system:ordererror:batchRepair")
    @Log(title = "批量补单", businessType = BusinessType.batchRepair)
    @PostMapping( "/batchRepair")
    @ResponseBody
    public AjaxResult batchRepair(String ids)
    {
        return toAjax(sysOrderService.batchRepairSysOrderByIds(ids));
    }

    /**
     * 批量撤单
     */
    @RequiresPermissions("system:ordererror:batchCancel")
    @Log(title = "批量撤单", businessType = BusinessType.batchCancel)
    @PostMapping( "/batchCancel")
    @ResponseBody
    public AjaxResult batchCancel(String ids)
    {
        return toAjax(sysOrderService.batchCancelSysOrderByIds(ids));
    }









    /**
     * 单个补单
     */
    @RequiresPermissions("system:ordererror:batchRepair")
    @PostMapping( "/repair")
    @ResponseBody
    public AjaxResult repair(Long id)
    {
        SysOrder sysOrder = sysOrderService.selectSysOrderById(id);
        if(sysOrder == null){
            error("订单记录不存在id="+id);
        }
        if(sysOrder.getSysOrderStatusApi()!=2){
            error("订单记录id="+id+"不是充值失败数据");
        }



        if(sysOrder.getManuaDoStatus()==2){
            error("订单记录id="+id+"处理状态=已处理");
        }
        sysOrder.setSysOrderStatus(2);   //状态   0=等待中 1=充值中 2=充值成功 3=取消充值 4=渠道充值失败
        sysOrder.setManuaDoStatus(2);    //人工处理状态 1=待处理 2=已处理
        sysOrder.setRepairOrderStatus(1);//补单状态 1=已补单 2=未补单
        ;
        return toAjax(sysOrderService.updateSysOrder(sysOrder));
    }

    /**
     * 单个取消补单
     */
    @RequiresPermissions("system:ordererror:batchRepair")
    @PostMapping( "/repairRedo")
    @ResponseBody
    public AjaxResult repairRedo(Long id)
    {
        SysOrder sysOrder = sysOrderService.selectSysOrderById(id);
        if(sysOrder == null){
            error("订单记录不存在id="+id);
        }
        if(sysOrder.getSysOrderStatusApi()!=2){
            error("订单记录id="+id+"不是充值失败数据");
        }


        if(sysOrder.getManuaDoStatus()==1){
            error("订单记录id="+id+"处理状态=未处理");
        }
        sysOrder.setSysOrderStatus(4);    //状态   0=等待中 1=充值中 2=充值成功 3=取消充值 4=渠道充值失败
        sysOrder.setManuaDoStatus(1);     //人工处理状态 1=待处理 2=已处理
        sysOrder.setRepairOrderStatus(2); //补单状态 1=已补单 2=未补单
        ;
        return toAjax(sysOrderService.updateSysOrder(sysOrder));
    }



    /**
     * 单个撤单
     */
    @RequiresPermissions("system:ordererror:batchCancel")
    @PostMapping( "/cancel")
    @ResponseBody
    public AjaxResult cancel(Long id)
    {
        SysOrder sysOrder = sysOrderService.selectSysOrderById(id);
        if(sysOrder == null){
            error("订单记录不存在id="+id);
        }
        if(sysOrder.getSysOrderStatusApi()!=2){
            error("订单记录id="+id+"不是充值失败数据");
        }


        if(sysOrder.getManuaDoStatus()==2){
            error("订单记录id="+id+"处理状态=已处理");
        }
        sysOrder.setSysOrderStatus(3);     //状态   0=等待中 1=充值中 2=充值成功 3=取消充值 4=渠道充值失败
        sysOrder.setManuaDoStatus(2);      //人工处理状态 1=待处理 2=已处理
        sysOrder.setCancelOrderStatus(1);  //撤单状态 1=已撤单 2=未撤单


        return toAjax(sysOrderService.updateSysOrder(sysOrder));
    }

    /**
     * 单个取消撤单
     */
    @RequiresPermissions("system:ordererror:batchCancel")
    @PostMapping( "/cancelRedo")
    @ResponseBody
    public AjaxResult cancelRedo(Long id)
    {
        SysOrder sysOrder = sysOrderService.selectSysOrderById(id);
        if(sysOrder == null){
            error("订单记录不存在id="+id);
        }
        if(sysOrder.getSysOrderStatusApi()!=2){
            error("订单记录id="+id+"不是充值失败数据");
        }

        if(sysOrder.getManuaDoStatus()==1){
            error("订单记录id="+id+"处理状态=未处理");
        }
        sysOrder.setSysOrderStatus(4);      //状态   0=等待中 1=充值中 2=充值成功 3=取消充值 4=渠道充值失败
        sysOrder.setManuaDoStatus(1);       //人工处理状态 1=待处理 2=已处理
        sysOrder.setCancelOrderStatus(2);  //撤单状态 1=已撤单 2=未撤单

        return toAjax(sysOrderService.updateSysOrder(sysOrder));
    }




}
